﻿using IDAdmin.Common;
using IDAdmin.Lib.DataLayer;
using IDAdmin.Lib.UI;
using IDAdmin.Lib.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IDAdmin.Pages
{
    public partial class LevelStatistics : BasePage
    {
        public LevelStatistics()
            : base(Lib.AppFunctions.LEVELDIS)
        {

        }
        protected override void Page_Load(object sender, EventArgs e)
        {
            if (!IsLogin())
            {
                RedirectToLogOn();
            }
            else if (!CheckRight())
            {
                RedirectToDeniedMessage();
            }
            else
            {
                changeStatus();
            }
        }

        protected void countButton_Click(object sender, EventArgs e)
        {
            DateTime dtFormDate = IDAdmin.Common.ConverterWorkData.convertStringToDateTime(countToolBar.beginDateTextBox.Text);
            DateTime dtToDate = IDAdmin.Common.ConverterWorkData.convertStringToDateTime(countToolBar.toDateTextBox.Text);
            List<DateTime> dateTimeList = IDAdmin.Common.ConverterWorkData.getDateListBetween(dtFormDate, dtToDate);
            dataPanel.Controls.Clear();
            System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
            watch.Start();
            dataPanel.Controls.Add(getLevelStatistics(WebDB.getCharBaseData(dateTimeList)));
            watch.Stop();
            double elapsedMS = watch.ElapsedMilliseconds;
            measureLB.Text = elapsedMS.ToString() + "miliseconds";
        }
        public static TableCell createServerTableCell(string text, Unit width, HorizontalAlign hAlign, string cssClass, int columnSpan)
        {
            TableCell cell = new TableCell();
            cell.Text = text;
            cell.Width = width;
            cell.CssClass = cssClass;
            cell.HorizontalAlign = hAlign;
            cell.ColumnSpan = columnSpan;
            return cell;
        }
        private Table getLevelStatistics(Dictionary<string, List<CharBase>> data)
        {
            List<string> serverList = new List<string>();
            serverList = data.Keys.ToList();
            serverList = serverList.OrderBy(o => o).ToList();
            List<LevelStatisticsData> lsList = new List<LevelStatisticsData>();
            List<CharBase> dataList = new List<CharBase>();
            foreach (var item in data)
            {
                foreach (var item2 in item.Value)
                    dataList.Add(new CharBase() { Level = item2.Level });
            }

            var levelList = (from tb in dataList select new { tb.Level }).Distinct().OrderBy(o => int.Parse(o.Level)).ToList();
            var levelAndPeopleNumList = (from tb in dataList group tb by new { tb.Level } into grp select new { Level = grp.Key.Level, Num = grp.Count() }).ToList();
            var sumofServerList = new[] { new { server = "", sum = "" } }.ToList();
            sumofServerList.RemoveAt(0);

            foreach (var item2 in data)
            {
                sumofServerList.Add(new { server = item2.Key, sum = item2.Value.Count().ToString() });
            }
            //sumofServerList=(from tb in data group tb by new {tb.Key} into grp select new {server=grp.Key,sum=grp.})
            int peopleNumSum = 0;
            foreach (var level in levelList)
            {
                LevelStatisticsData ls = new LevelStatisticsData();
                ls.Level = level.Level;
                ls.PeopleNum = levelAndPeopleNumList.Where(w => w.Level.Equals(level.Level)).FirstOrDefault().Num.ToString();
                peopleNumSum += int.Parse(ls.PeopleNum);
                ls.PeopleNumPercent = ((double.Parse(ls.PeopleNum) * 100) / (double)dataList.Count()).ToString("0.00") + "%";
                foreach (var item2 in data)
                {
                    PeopleNumAndPercentServer pnps = new PeopleNumAndPercentServer();
                    pnps.GameZone = item2.Key;
                    pnps.PeopleNum = (from tb in item2.Value where tb.Level.Equals(level.Level) select tb).Count().ToString();
                    pnps.PeopleNumPercent = ((double.Parse(pnps.PeopleNum) * 100) / double.Parse(sumofServerList.Where(w => w.server.Equals(item2.Key)).FirstOrDefault().sum)).ToString("0.00") + "%";
                    ls.PeopleNumAndPercentServerList.Add(pnps);
                }
                lsList.Add(ls);
            }

            //DataTable dt = ConverterWorkData.ToDataTable(lsList, 4, serverList.Count * 2);

            Table table = new Table();
            table.CssClass = "table1";
            table.CellSpacing = 1;
            TableRow rowHeader = new TableRow();
            rowHeader.Cells.AddRange
            (
                new TableCell[]
                {     
                    UIHelpers.CreateTableCell("STT",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader","2"),
                    UIHelpers.CreateTableCell("Cấp độ",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader","2"),
                    UIHelpers.CreateTableCell("Số người",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader","2"),
                    UIHelpers.CreateTableCell("Tỷ lệ",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader","2")
                }
            );

            foreach (var server in serverList)
            {
                rowHeader.Cells.Add(createServerTableCell(server, Unit.Percentage(5), HorizontalAlign.Center, "cellHeader", 2));
            }
            table.Rows.Add(rowHeader);

            TableRow rowHeader2 = new TableRow();
            foreach (var server in serverList)
            {
                rowHeader2.Cells.AddRange(
                    new TableCell[]{
                UIHelpers.CreateTableCell("Số người", Unit.Percentage(5), HorizontalAlign.Center, "cellHeader"),
                UIHelpers.CreateTableCell("Tỷ lệ", Unit.Percentage(5), HorizontalAlign.Center, "cellHeader")
                    });
            }
            table.Rows.Add(rowHeader2);

            try
            {
                if (lsList == null || lsList.Count == 0)
                {
                    TableRow rowEmpty = new TableRow();
                    rowEmpty.Cells.Add(UIHelpers.CreateTableCell("<p>Không có dữ liệu!</p>", HorizontalAlign.Center, "cell1", 4));
                    table.Rows.Add(rowEmpty);
                }
                else
                {
                    //labelCountOfUser.Text = string.Format("{0:N0}", dt.Rows.Count);
                    string css;
                    int stt = 0;
                    int peopleSum = 0;
                    foreach (var dr in lsList)
                    {
                        peopleSum += int.Parse(dr.PeopleNum);
                        stt += 1;
                        css = stt % 2 == 0 ? "cell1" : "cell2";
                        TableRow row = new TableRow();
                        row.Cells.AddRange
                        (
                            new TableCell[]
                           {
                                    UIHelpers.CreateTableCell(stt.ToString(),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.Level),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.PeopleNum),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.PeopleNumPercent),HorizontalAlign.Center,css)
                           }
                        );
                        foreach (var item in dr.PeopleNumAndPercentServerList.OrderBy(o => o.GameZone).ToList())
                        {
                            row.Cells.Add(UIHelpers.CreateTableCell(Converter.ToString(item.PeopleNum), HorizontalAlign.Center, css));
                            row.Cells.Add(UIHelpers.CreateTableCell(Converter.ToString(item.PeopleNumPercent), HorizontalAlign.Center, css));
                        }
                        table.Rows.Add(row);
                    }
                    TableRow lastRow = new TableRow();
                    lastRow.Cells.AddRange
                    (
                        new TableCell[]
                           {
                                    UIHelpers.CreateTableCell(stt.ToString(),HorizontalAlign.Center,"cell1"),
                                    UIHelpers.CreateTableCell("Tổng người",HorizontalAlign.Center,"cell1"),
                                    UIHelpers.CreateTableCell(peopleSum.ToString(),HorizontalAlign.Center,"cell1"),
                                    UIHelpers.CreateTableCell("",HorizontalAlign.Center,"cell1"),
                                     UIHelpers.CreateTableCell("",HorizontalAlign.Center,"cell1"),
                                      UIHelpers.CreateTableCell("",HorizontalAlign.Center,"cell1")
                           }
                    );
                    table.Rows.Add(lastRow);
                }
            }
            catch (Exception ex)
            {
            }
            return table;
        }
        public static void cronLevelDis(DateTime date)
        {
            List<LevelStatisticsData> lsList = new List<LevelStatisticsData>();

            //DateTime date = DataToolUtil.convertStringToDateTime(txtCountDate.Text);
            LeveStatsWebDB lsWWebDB = new LeveStatsWebDB();
            Dictionary<string, List<LevelAndPeopleNum>> result = lsWWebDB.getLevelStats(date);
            List<string> serverList = new List<string>();
            serverList = result.Keys.ToList();
            serverList = serverList.OrderBy(o => o).ToList();
            var allList = result.SelectMany(x => x.Value);
            List<string> levelList = allList.Select(s => s.Level).OrderBy(o => int.Parse(o)).Distinct().ToList();
            int peopleNumSum = 0;
            int peopleNumAll = allList.Sum(s => int.Parse(s.PeopleNum));

            foreach (var level in levelList)
            {
                LevelStatisticsData ls = new LevelStatisticsData();
                ls.DateTime = date;
                ls.Level = level;
                ls.PeopleNum = allList.Where(w => w.Level.Equals(level)).Sum(s => int.Parse(s.PeopleNum)).ToString();
                peopleNumSum += int.Parse(ls.PeopleNum);
                ls.PeopleNumPercent = ((double.Parse(ls.PeopleNum) * 100) / (double)peopleNumAll).ToString("0.00") + "%";
                lsList.Add(ls);
                foreach (var item2 in result.Keys)
                {
                    int peopleSum = result[item2].Sum(s => int.Parse(s.PeopleNum));
                    PeopleNumAndPercentServer pnps = new PeopleNumAndPercentServer();
                    pnps.Level = level;
                    pnps.DateTime = date;
                    pnps.GameZone = item2;
                    pnps.Server = item2;
                    var peopleNumVar = (from tb in result[item2] where tb.Level.Equals(level) select tb.PeopleNum).FirstOrDefault();
                    if (peopleNumVar != null)
                    {
                        pnps.PeopleNum = peopleNumVar.ToString();
                    }
                    if (string.IsNullOrEmpty(pnps.PeopleNum) == false && peopleSum != 0)
                    {
                        pnps.PeopleNumPercent = ((double.Parse(pnps.PeopleNum) * 100) / (double)peopleSum).ToString("0.00") + "%";
                    }
                    if (string.IsNullOrEmpty(pnps.PeopleNum) == false && string.IsNullOrEmpty(pnps.PeopleNumPercent) == false)
                    {
                        ls.PeopleNumAndPercentServerList.Add(pnps);
                    }
                }

            }
            lsWWebDB.insertLevelDis(lsList, date);
        }
        private Table getLevelStatsTable(DateTime date, int type)
        {
            List<LevelStatisticsData> lsList = new List<LevelStatisticsData>();
            LeveStatsWebDB lsWWebDB = new LeveStatsWebDB();
            Dictionary<string, List<LevelAndPeopleNum>> result = lsWWebDB.getLevelStats(date);
            List<string> serverList = new List<string>();
            serverList = result.Keys.ToList();
            serverList = serverList.OrderBy(o => o).ToList();
            var allList = result.SelectMany(x => x.Value);
            List<string> levelList = allList.Select(s => s.Level).OrderBy(o => int.Parse(o)).Distinct().ToList();
            int peopleNumSum = 0;
            int peopleNumAll = allList.Sum(s => int.Parse(s.PeopleNum));

            foreach (var level in levelList)
            {
                LevelStatisticsData ls = new LevelStatisticsData();
                ls.DateTime = date;
                ls.Level = level;
                ls.PeopleNum = allList.Where(w => w.Level.Equals(level)).Sum(s => int.Parse(s.PeopleNum)).ToString();
                peopleNumSum += int.Parse(ls.PeopleNum);
                ls.PeopleNumPercent = ((double.Parse(ls.PeopleNum) * 100) / (double)peopleNumAll).ToString("0.00") + "%";
                lsList.Add(ls);
                foreach (var item2 in result.Keys)
                {
                    int peopleSum = result[item2].Sum(s => int.Parse(s.PeopleNum));
                    PeopleNumAndPercentServer pnps = new PeopleNumAndPercentServer();
                    pnps.Level = level;
                    pnps.DateTime = date;
                    pnps.GameZone = item2;
                    pnps.Server = item2;
                    var peopleNumVar = (from tb in result[item2] where tb.Level.Equals(level) select tb.PeopleNum).FirstOrDefault();
                    if (peopleNumVar != null)
                    {
                        pnps.PeopleNum = peopleNumVar.ToString();
                    }
                    if (string.IsNullOrEmpty(pnps.PeopleNum) == false && peopleSum != 0)
                    {
                        pnps.PeopleNumPercent = ((double.Parse(pnps.PeopleNum) * 100) / (double)peopleSum).ToString("0.00") + "%";
                    }
                    if (string.IsNullOrEmpty(pnps.PeopleNum) == false && string.IsNullOrEmpty(pnps.PeopleNumPercent) == false)
                    {
                        ls.PeopleNumAndPercentServerList.Add(pnps);
                    }
                }

            }
            lsWWebDB.insertLevelDis(lsList, date);

            Table table = new Table();
            if (type == 0)
            {
                table.CssClass = "table1";
                table.CellSpacing = 1;
                TableRow rowHeader = new TableRow();
                rowHeader.Cells.AddRange
                (
                    new TableCell[]
                {     
                    UIHelpers.CreateTableCell("STT",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader","2"),
                    UIHelpers.CreateTableCell("Cấp độ",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader","2"),
                    UIHelpers.CreateTableCell("Số người",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader","2"),
                    UIHelpers.CreateTableCell("Tỷ lệ",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader","2")
                }
                );

                foreach (var server in serverList)
                {
                    rowHeader.Cells.Add(createServerTableCell(server, Unit.Percentage(5), HorizontalAlign.Center, "cellHeader", 2));
                }
                table.Rows.Add(rowHeader);

                TableRow rowHeader2 = new TableRow();
                foreach (var server in serverList)
                {
                    rowHeader2.Cells.AddRange(
                        new TableCell[]{
                UIHelpers.CreateTableCell("Số người", Unit.Percentage(5), HorizontalAlign.Center, "cellHeader"),
                UIHelpers.CreateTableCell("Tỷ lệ", Unit.Percentage(5), HorizontalAlign.Center, "cellHeader")
                    });
                }
                table.Rows.Add(rowHeader2);

                try
                {
                    if (lsList == null || lsList.Count == 0)
                    {
                        TableRow rowEmpty = new TableRow();
                        rowEmpty.Cells.Add(UIHelpers.CreateTableCell("<p>Không có dữ liệu!</p>", HorizontalAlign.Center, "cell1", 4));
                        table.Rows.Add(rowEmpty);
                    }
                    else
                    {
                        //labelCountOfUser.Text = string.Format("{0:N0}", dt.Rows.Count);
                        string css;
                        int stt = 0;
                        int peopleSum = 0;
                        foreach (var dr in lsList)
                        {
                            peopleSum += int.Parse(dr.PeopleNum);
                            stt += 1;
                            css = stt % 2 == 0 ? "cell1" : "cell2";
                            TableRow row = new TableRow();
                            row.Cells.AddRange
                            (
                                new TableCell[]
                           {
                                    UIHelpers.CreateTableCell(stt.ToString(),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.Level),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.PeopleNum),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.PeopleNumPercent),HorizontalAlign.Center,css)
                           }
                            );
                            foreach (var item in dr.PeopleNumAndPercentServerList.OrderBy(o => o.GameZone).ToList())
                            {
                                row.Cells.Add(UIHelpers.CreateTableCell(Converter.ToString(item.PeopleNum), HorizontalAlign.Center, css));
                                row.Cells.Add(UIHelpers.CreateTableCell(Converter.ToString(item.PeopleNumPercent), HorizontalAlign.Center, css));
                            }
                            table.Rows.Add(row);
                        }
                        TableRow lastRow = new TableRow();
                        lastRow.Cells.AddRange
                        (
                            new TableCell[]
                           {
                                    UIHelpers.CreateTableCell(stt.ToString(),HorizontalAlign.Center,"cell1"),
                                    UIHelpers.CreateTableCell("Tổng người",HorizontalAlign.Center,"cell1"),
                                    UIHelpers.CreateTableCell(peopleNumAll.ToString(),HorizontalAlign.Center,"cell1"),
                                    UIHelpers.CreateTableCell("",HorizontalAlign.Center,"cell1"),
                                     UIHelpers.CreateTableCell("",HorizontalAlign.Center,"cell1"),
                                      UIHelpers.CreateTableCell("",HorizontalAlign.Center,"cell1")
                           }
                        );
                        table.Rows.Add(lastRow);
                    }
                }
                catch (Exception ex)
                {
                }
            }
            return table;
        }
        protected void buttonCountSaveLS_Click(object sender, EventArgs e)
        {
            if (DropDownListType.SelectedItem.Value == "0")
            {
                dataPanel.Controls.Clear();
                DateTime date = DataToolUtil.convertStringToDateTime(txtCountDate.Text);
                dataPanel.Controls.Add(getLevelStatsTable(date, 0));
            }
            else
            {
                dataPanel.Controls.Clear();
                DateTime date = DataToolUtil.convertStringToDateTime(txtCountDate.Text);
                dataPanel.Controls.Add(getLevelCALINStatsTable(date,0));
            }
        }

        protected void buttonCountLS_Click(object sender, EventArgs e)
        {
            if (DropDownListType.SelectedItem.Value == "0")
            {
                dataPanel.Controls.Clear();
                dataPanel.Controls.Add(getLevelDisTable());
            }
            else
            {
                dataPanel.Controls.Clear();
                dataPanel.Controls.Add(getFastLevelCALINTable());
            }
        }
        private Table getLevelCALINStatsTable(DateTime date, int type)
        {
            //DateTime date = IDAdmin.Common.ConverterWorkData.convertStringToDateTime(txtCountDate.Text);
            LevelDisAccLossIncreNewWebDB ldalinWebDB = new LevelDisAccLossIncreNewWebDB();
            ldalinWebDB.insertCALIN(date);
            List<string> levelList = new List<string>();
            List<LevelAndPeopleNum> lvAndPeoNumList = ldalinWebDB.getStats(date, out levelList);
            ldalinWebDB.insertStatsData(lvAndPeoNumList,date);
            if (type == 0)
            {
                if (string.IsNullOrEmpty(txtLevelMax.Text) == false)
                {
                    int levelMax = int.Parse(txtLevelMax.Text);
                    for (int i = 1; i <= levelMax; i++)
                    {
                        var has = levelList.Where(w => w.Equals(i.ToString())).FirstOrDefault();
                        if (has == null)
                        {
                            lvAndPeoNumList.Add(new LevelAndPeopleNum() { Level = i.ToString(), PeopleNum = "0" });
                        }
                    }
                }
                lvAndPeoNumList = lvAndPeoNumList.OrderBy(o => int.Parse(o.Level)).ToList();
            }
            //List<LevelAndPeopleNum> lvAndPeoNumList = lls.getDataStats(date).OrderBy(o => int.Parse(o.Level)).ToList();
            //lls.insertLevelLoss(lvAndPeoNumList,date);

            Table table = new Table();
            if (type == 0)
            {
                table.CssClass = "table1";
                table.CellSpacing = 1;
                TableRow rowHeader = new TableRow();
                rowHeader.Cells.AddRange
                (
                    new TableCell[]
                {     
                    UIHelpers.CreateTableCell("STT",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                     UIHelpers.CreateTableCell("Level",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                       UIHelpers.CreateTableCell("People Number",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader")
                    //UIHelpers.CreateTableCell("CcuServerList",Unit.Percentage(6),HorizontalAlign.Center, "cellHeader"),  
                    //UIHelpers.CreateTableCell(dt.Columns[5].ColumnName,Unit.Percentage(6),HorizontalAlign.Center, "cellHeader"),  
                    //UIHelpers.CreateTableCell(dt.Columns[6].ColumnName,Unit.Percentage(6),HorizontalAlign.Center, "cellHeader")
                }
                );

                table.Rows.Add(rowHeader);
                try
                {
                    if (lvAndPeoNumList == null || lvAndPeoNumList.Count == 0)
                    {
                        TableRow rowEmpty = new TableRow();
                        rowEmpty.Cells.Add(UIHelpers.CreateTableCell("<p>Không có dữ liệu!</p>", HorizontalAlign.Center, "cell1", 4));
                        table.Rows.Add(rowEmpty);
                    }
                    else
                    {
                        //labelCountOfUser.Text = string.Format("{0:N0}", dt.Rows.Count);
                        string css;
                        int stt = 0;
                        foreach (LevelAndPeopleNum dr in lvAndPeoNumList)
                        {
                            stt += 1;
                            css = stt % 2 == 0 ? "cell1" : "cell2";
                            TableRow row = new TableRow();
                            row.Cells.AddRange
                            (
                                new TableCell[]
                           {
                                    UIHelpers.CreateTableCell(stt.ToString(),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.Level),HorizontalAlign.Left,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.PeopleNum),HorizontalAlign.Left,css)
                                    //UIHelpers.CreateTableCell(Converter.ToString(dr[dt.Columns[5].ColumnName]),HorizontalAlign.Left,css),
                                    //UIHelpers.CreateTableCell(Converter.ToString(dr[dt.Columns[6].ColumnName]),HorizontalAlign.Left,css),

                           }
                            );

                            table.Rows.Add(row);
                        }
                    }
                }
                catch (Exception ex)
                {
                }
            }
            return table;
        }
        private Table getLevelDisTable()
        {
            LeveStatsWebDB lsWWebDB = new LeveStatsWebDB();
            DateTime date = DataToolUtil.convertStringToDateTime(txtCountDate.Text);
            List<string> serverList = new List<string>();
            int peopleNumAll = 0;
            List<LevelStatisticsData> lsList = lsWWebDB.getLevelDisData(date.ToString("yyyy-MM-dd 00:00:00"), date.ToString("yyyy-MM-dd 23:59:59"), out serverList, out peopleNumAll);
            serverList = serverList.OrderBy(o => o).ToList();
            lsList = lsList.OrderBy(o => int.Parse(o.Level)).ToList();
            Table table = new Table();
            table.CssClass = "table1";
            table.CellSpacing = 1;
            TableRow rowHeader = new TableRow();
            rowHeader.Cells.AddRange
            (
                new TableCell[]
                {     
                    UIHelpers.CreateTableCell("STT",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader","2"),
                    UIHelpers.CreateTableCell("Cấp độ",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader","2"),
                    UIHelpers.CreateTableCell("Số người",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader","2"),
                    UIHelpers.CreateTableCell("Tỷ lệ",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader","2")
                }
            );

            foreach (var server in serverList)
            {
                rowHeader.Cells.Add(createServerTableCell(server, Unit.Percentage(5), HorizontalAlign.Center, "cellHeader", 2));
            }
            table.Rows.Add(rowHeader);

            TableRow rowHeader2 = new TableRow();
            foreach (var server in serverList)
            {
                rowHeader2.Cells.AddRange(
                    new TableCell[]{
                UIHelpers.CreateTableCell("Số người", Unit.Percentage(5), HorizontalAlign.Center, "cellHeader"),
                UIHelpers.CreateTableCell("Tỷ lệ", Unit.Percentage(5), HorizontalAlign.Center, "cellHeader")
                    });
            }
            table.Rows.Add(rowHeader2);

            try
            {
                if (lsList == null || lsList.Count == 0)
                {
                    TableRow rowEmpty = new TableRow();
                    rowEmpty.Cells.Add(UIHelpers.CreateTableCell("<p>Không có dữ liệu!</p>", HorizontalAlign.Center, "cell1", 4));
                    table.Rows.Add(rowEmpty);
                }
                else
                {
                    //labelCountOfUser.Text = string.Format("{0:N0}", dt.Rows.Count);
                    string css;
                    int stt = 0;
                    int peopleSum = 0;
                    foreach (var dr in lsList)
                    {
                        peopleSum += int.Parse(dr.PeopleNum);
                        stt += 1;
                        css = stt % 2 == 0 ? "cell1" : "cell2";
                        TableRow row = new TableRow();
                        row.Cells.AddRange
                        (
                            new TableCell[]
                           {
                                    UIHelpers.CreateTableCell(stt.ToString(),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.Level),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.PeopleNum),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.PeopleNumPercent),HorizontalAlign.Center,css)
                           }
                        );
                        foreach (var item in dr.PeopleNumAndPercentServerList.OrderBy(o => o.GameZone).ToList())
                        {
                            row.Cells.Add(UIHelpers.CreateTableCell(Converter.ToString(item.PeopleNum), HorizontalAlign.Center, css));
                            row.Cells.Add(UIHelpers.CreateTableCell(Converter.ToString(item.PeopleNumPercent), HorizontalAlign.Center, css));
                        }
                        table.Rows.Add(row);
                    }
                    TableRow lastRow = new TableRow();
                    lastRow.Cells.AddRange
                    (
                        new TableCell[]
                           {
                                    UIHelpers.CreateTableCell(stt.ToString(),HorizontalAlign.Center,"cell1"),
                                    UIHelpers.CreateTableCell("Tổng người",HorizontalAlign.Center,"cell1"),
                                    UIHelpers.CreateTableCell(peopleNumAll.ToString(),HorizontalAlign.Center,"cell1"),
                                    UIHelpers.CreateTableCell("",HorizontalAlign.Center,"cell1"),
                                     UIHelpers.CreateTableCell("",HorizontalAlign.Center,"cell1"),
                                      UIHelpers.CreateTableCell("",HorizontalAlign.Center,"cell1")
                           }
                    );
                    table.Rows.Add(lastRow);
                }
            }
            catch (Exception ex)
            {
            }
            return table;
        }
        private Table getFastLevelCALINTable()
        {
            DateTime date = IDAdmin.Common.ConverterWorkData.convertStringToDateTime(txtCountDate.Text);
            LevelDisAccLossIncreNewWebDB ldalinWebDB = new LevelDisAccLossIncreNewWebDB();
            List<string> levelList = new List<string>();
            List<LevelAndPeopleNum> lvAndPeoNumList = ldalinWebDB.getFastStatsData(date.ToString("yyyy-MM-dd") + " 00:00:00", date.ToString("yyyy-MM-dd") + " 23:59:59", levelList);
            if (string.IsNullOrEmpty(txtLevelMax.Text) == false)
            {
                int levelMax = int.Parse(txtLevelMax.Text);
                for (int i = 1; i <= levelMax; i++)
                {
                    var has = levelList.Where(w => w.Equals(i.ToString())).FirstOrDefault();
                    if (has == null)
                    {
                        lvAndPeoNumList.Add(new LevelAndPeopleNum() { Level = i.ToString(), PeopleNum = "0" });
                    }
                }
            }
            lvAndPeoNumList = lvAndPeoNumList.OrderBy(o => int.Parse(o.Level)).ToList();
            //List<LevelAndPeopleNum> lvAndPeoNumList = lssWebDB.getFastStatsData(date.ToString("yyyy-MM-dd") + " 00:00:00", date.ToString("yyyy-MM-dd" + " 23:59:59"));
            //lvAndPeoNumList = lvAndPeoNumList.OrderBy(o => int.Parse(o.Level)).ToList();
            Table table = new Table();
            table.CssClass = "table1";
            table.CellSpacing = 1;
            TableRow rowHeader = new TableRow();
            rowHeader.Cells.AddRange
            (
                new TableCell[]
                {     
                    UIHelpers.CreateTableCell("STT",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                     UIHelpers.CreateTableCell("Level",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader"),
                       UIHelpers.CreateTableCell("People Number",Unit.Percentage(5), HorizontalAlign.Center,"cellHeader")
                    //UIHelpers.CreateTableCell("CcuServerList",Unit.Percentage(6),HorizontalAlign.Center, "cellHeader"),  
                    //UIHelpers.CreateTableCell(dt.Columns[5].ColumnName,Unit.Percentage(6),HorizontalAlign.Center, "cellHeader"),  
                    //UIHelpers.CreateTableCell(dt.Columns[6].ColumnName,Unit.Percentage(6),HorizontalAlign.Center, "cellHeader")
                }
            );

            table.Rows.Add(rowHeader);
            try
            {
                if (lvAndPeoNumList == null || lvAndPeoNumList.Count == 0)
                {
                    TableRow rowEmpty = new TableRow();
                    rowEmpty.Cells.Add(UIHelpers.CreateTableCell("<p>Không có dữ liệu!</p>", HorizontalAlign.Center, "cell1", 4));
                    table.Rows.Add(rowEmpty);
                }
                else
                {
                    //labelCountOfUser.Text = string.Format("{0:N0}", dt.Rows.Count);
                    string css;
                    int stt = 0;
                    foreach (LevelAndPeopleNum dr in lvAndPeoNumList)
                    {
                        stt += 1;
                        css = stt % 2 == 0 ? "cell1" : "cell2";
                        TableRow row = new TableRow();
                        row.Cells.AddRange
                        (
                            new TableCell[]
                           {
                                    UIHelpers.CreateTableCell(stt.ToString(),HorizontalAlign.Center,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.Level),HorizontalAlign.Left,css),
                                    UIHelpers.CreateTableCell(Converter.ToString(dr.PeopleNum),HorizontalAlign.Left,css)
                                    //UIHelpers.CreateTableCell(Converter.ToString(dr[dt.Columns[5].ColumnName]),HorizontalAlign.Left,css),
                                    //UIHelpers.CreateTableCell(Converter.ToString(dr[dt.Columns[6].ColumnName]),HorizontalAlign.Left,css),

                           }
                        );

                        table.Rows.Add(row);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return table;
        }
        protected void ButtonCountSaveFromDtToDt_Click(object sender, EventArgs e)
        {
            dataPanel.Controls.Clear();
            DateTime dtFormDate = DataToolUtil.convertStringToDateTime(countToolBar.beginDateTextBox.Text);
            DateTime dtToDate = DataToolUtil.convertStringToDateTime(countToolBar.toDateTextBox.Text);
            List<DateTime> dateTimeList = DataToolUtil.getDateListBetween(dtFormDate, dtToDate);
            if (DropDownListType.SelectedItem.Value == "0")
            {
                foreach (DateTime date in dateTimeList)
                {
                    getLevelStatsTable(date, 1);
                }
            }
            else
            {
                foreach (DateTime date in dateTimeList)
                {
                    dataPanel.Controls.Add(getLevelCALINStatsTable(date, 1));
                }
            }
        }

        protected void DropDownListType_SelectedIndexChanged(object sender, EventArgs e)
        {
            changeStatus();
        }

        private void changeStatus()
        {
            if (DropDownListType.SelectedItem.Value == "1")
            {
                //txtLevelMax.Visible = true;
                levelMax.Visible = true;
            }
            else
            {
                //txtLevelMax.Visible = false;
                levelMax.Visible = false;
            }
        }
    }
}